﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SES.CMS;
using System.Data;

namespace Photo
{
    public partial class ListAlbum : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                gvListAlbum.DataSource = new AlbumBL().SelectAll();
                gvListAlbum.DataBind();
            }

        }

        public void gvListAlbum_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvListAlbum.PageIndex = e.NewPageIndex;
            LoadAlbum();
        }
        public void gvListAlbum_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                Xoafile(sender, e);
                XoaAnh(sender, e);
                new AlbumBL().Delete(new AlbumDO { AlbumID = Convert.ToInt32(gvListAlbum.DataKeys[e.RowIndex].Value) });
                Functions.Alert("Xóa thành công!", Request.Url.ToString());
            }
            catch (Exception i)
            {
                Functions.Alert(i.Message, Request.Url.ToString());
            }

        }

        private void XoaAnh(object sender, GridViewDeleteEventArgs e)
        {
            DataTable dtb = new IMGBL().SelectbyAlbum(Convert.ToInt32(gvListAlbum.DataKeys[e.RowIndex].Value));
            foreach (DataRow dr in dtb.Rows)
            {
                string duongdan = string.Format("{0}\\{1}", Server.MapPath("~/Media/"), dr["Url"].ToString());
                if (System.IO.File.Exists(duongdan))
                {
                    System.IO.File.Delete(duongdan);
                }
            }

        }

        private void Xoafile(object sender, GridViewDeleteEventArgs e)
        {
            AlbumDO album = new AlbumBL().Select(new AlbumDO { AlbumID = Convert.ToInt32(gvListAlbum.DataKeys[e.RowIndex].Value) });
            string duongdan = string.Format("{0}\\{1}", Server.MapPath("~/Media/"), album.AnhDaiDien);
            if (System.IO.File.Exists(duongdan))
            {
                System.IO.File.Delete(duongdan);
            }

         ///xoa anh detail
            DataTable dt = new IMGBL().SelectbyAlbum(int.Parse(album.AlbumID.ToString()));
            foreach (DataRow dr in dt.Rows)
            {
                if (string.IsNullOrEmpty(dr["url"].ToString()))
                {
                    duongdan = string.Format("{0}\\{1}", Server.MapPath("~/Media/"), dr["url"].ToString());
                    if (System.IO.File.Exists(duongdan))
                    {
                        System.IO.File.Delete(duongdan);
                    }
                    // xóa CSDL detail
                    new IMGBL().Delete(new IMGDO { ImgID = Convert.ToInt32(dr["ImgID"].ToString()) });
                }
            }

        }
        public void gvListAlbum_SelectedIndexChanged(object sender, EventArgs e)
        {
            int AlbumID = int.Parse(gvListAlbum.DataKeys[gvListAlbum.SelectedIndex].Value.ToString());
            Response.Redirect("Album.aspx?Page=Album&AlbumID=" + AlbumID.ToString());
        }
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Ultility.RedirectPage("Album.aspx");
        }

        private void LoadAlbum()
        {
            gvListAlbum.DataSource = new AlbumBL().SelectAll();
            gvListAlbum.DataBind();
        }

        protected void uploadAnh_Click(object sender, GridViewDeleteEventArgs e)
        {
            Functions.Alert(gvListAlbum.DataKeys[gvListAlbum.SelectedIndex].Value.ToString(), Request.Url.ToString());
        }

    }
}